<template>
  <div class="app-container">
    <!-- Filter -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="接口名称" style="width: 200px;" class="filter-item" />
      <el-select v-model="listQuery.project" filterable clearable placeholder="请选择" class="filter-item" value="">
        <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button type="primary" class="filter-item" @click="handleFilter">查询</el-button>
      <router-link :to="'/Keyword/interface/create'">
        <el-button type="primary" style="margin-left: 20px" class="filter-item">新增</el-button>
      </router-link>
    </div>
    <!-- Table -->
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column label="ID" prop="id" width="80px">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接口名称" prop="name">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求方法" prop="method">
        <template slot-scope="scope">
          <span>{{ scope.row.method }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求url" prop="url">
        <template slot-scope="scope">
          <span>{{ scope.row.url }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="add_time">
        <template slot-scope="scope">
          <span>{{ changeDate(scope.row.add_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" style="width: 120px">
        <template slot-scope="{row}">
          <router-link :to="'/Keyword/interface/edit/' + row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" />
  </div>
</template>

<script>
import { getInterfaceList, deleteInterface } from '@/api/interface'
import { getProjectList } from '@/api/project'
import Pagination from '@/components/Pagination/index'
import { dateFormat } from '@/utils/data'
export default {
  name: 'List',
  components: { Pagination },
  data() {
    return {
      list: [],
      projectList: [],
      total: 0,
      loading: false,
      listQuery: {
        name: '',
        project: '',
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.getList()
    this.getProjectList()
  },
  methods: {
    changeDate: function(attr) {
      return dateFormat(attr)
    },
    getList() {
      this.loading = true
      getInterfaceList(this.listQuery).then((response) => {
        this.list = response.results
        this.total = response.count
        this.loading = false
      })
    },
    getProjectList() {
      const params = {
        page: 1,
        limit: 30
      }
      getProjectList(params).then((response) => {
        this.projectList = response.results
      })
    },
    handleFilter() {
      this.getList()
    },
    handleDelete(row) {
      this.loading = true
      deleteInterface(row.id).then(() => {
        this.handleFilter()
      })
    }
  }
}
</script>

<style scoped>

</style>
